package cn.iocoder.yudao.module.energy.controller.admin.upload;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.energy.controller.admin.upload.vo.*;
import cn.iocoder.yudao.module.energy.dal.dataobject.upload.UploadDO;
import cn.iocoder.yudao.module.energy.service.upload.UploadService;

@Tag(name = "管理后台 - 文件上传")
@RestController
@RequestMapping("/energy/upload")
@Validated
public class UploadController {

    @Resource
    private UploadService uploadService;

    @PostMapping("/create")
    @Operation(summary = "创建文件上传")
    @PreAuthorize("@ss.hasPermission('energy:upload:create')")
    public CommonResult<Long> createUpload(@Valid @RequestBody UploadSaveReqVO createReqVO) {
        return success(uploadService.createUpload(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件上传")
    @PreAuthorize("@ss.hasPermission('energy:upload:update')")
    public CommonResult<Boolean> updateUpload(@Valid @RequestBody UploadSaveReqVO updateReqVO) {
        uploadService.updateUpload(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件上传")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:upload:delete')")
    public CommonResult<Boolean> deleteUpload(@RequestParam("id") Long id) {
        uploadService.deleteUpload(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件上传")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('energy:upload:query')")
    public CommonResult<UploadRespVO> getUpload(@RequestParam("id") Long id) {
        UploadDO upload = uploadService.getUpload(id);
        return success(BeanUtils.toBean(upload, UploadRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件上传分页")
    @PreAuthorize("@ss.hasPermission('energy:upload:query')")
    public CommonResult<PageResult<UploadRespVO>> getUploadPage(@Valid UploadPageReqVO pageReqVO) {
        PageResult<UploadDO> pageResult = uploadService.getUploadPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UploadRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件上传 Excel")
    @PreAuthorize("@ss.hasPermission('energy:upload:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUploadExcel(@Valid UploadPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UploadDO> list = uploadService.getUploadPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件上传.xls", "数据", UploadRespVO.class,
                        BeanUtils.toBean(list, UploadRespVO.class));
    }

}